package ch.usi.inf.pf2.cell;

import ch.usi.inf.pf2.cell.Recognizer;
import ch.usi.inf.pf2.cell.TokenType;
import ch.usi.inf.pf2.node.Addition;
import ch.usi.inf.pf2.node.Constant;
import ch.usi.inf.pf2.node.Node;
import ch.usi.inf.pf2.node.Subtraction;
import ch.usi.inf.pf2.cell.Parser;
/** 
 * A Cell ,each object in this class represent one cell(square) in SpreadSheet.
 * @author Elham.MenzerNasr.Esfahani@usi.ch
 */
public class Cell implements Node {

	private Node node;
	private static String str;
	private CellArray cellArray;

	//make empty Cell	  
	public Cell(CellArray cellArray)
	{
		str  = "";
		node = new Constant(0);
		this.cellArray=cellArray;
	}
	//interpret the String and create the Node for that	  
	public Cell(String str, CellArray cellArray)

	{
        this.str = str;
        this.cellArray=cellArray;
        makeNode();
	}

   /**it looks what kind of node it is and then it gives the right string to the parser and it stores 
	*the result in the cell
	*/
    private void makeNode() {
    	Parser parser = new Parser(cellArray);
		Recognizer recog = new Recognizer();
		recog.setText(str);
		recog.fetchNextToken();
		if (recog.getCurrentToken().getType()==TokenType.EQUAL) {
			node = parser.parseFormula(str.substring(1));
		} else if (recog.getCurrentToken().getType()==TokenType.FLOATINGPOINTNUM) {
			node = parser.parseFormula(str);
		} else {
			node = new Constant(0);
		}
    }

	public int textlength()
	{
		return str.length();
	}


	public static String getStr()
	{
		return str;
	}

	public Node getNode()
	{
		return node;
	}


	public void setStr(String str)

	{
		this.str=str;
		makeNode();
	} 

	public NumericValue evaluate()
	{
		return node.evaluate();
		
	}


}


